<template>
  <div class="loginContainer">
    <div class="loginBox">
      <!-- 头像框 -->
      <div class="headImg">
        <img src="../assets/images/clown.png" alt />
      </div>
      <!-- 用户登录框 -->
      <el-form :model="loginForm" ref="loginRef" :rules="loginRules">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-user"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loginVerify">登录</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 6, message: '长度在 2 到 6 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置整个表单
    resetForm() {
      this.$refs.loginRef.resetFields()
    },
    loginVerify() {
      this.$refs.loginRef.validate(async vaild => {
        // 如果提交空表单直接返回
        if (!vaild) return
        const res = await this.$http.post('/login', this.loginForm)
        if (res.meta.status !== 200) {
          return this.$message({
            showClose: true,
            message: '用户名或密码输入错误',
            type: 'error'
          })
        }
        this.$message({
          showClose: true,
          message: '登录成功',
          type: 'success'
        })
        // 用户登录成功时存储用户身份令牌
        sessionStorage.setItem('token', res.data.token)
        sessionStorage.setItem('person', JSON.stringify(res.data))
        // 使用编程式导航跳转到主页
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less" scope>
.loginContainer {
  background-color: #41b883;
  height: 100%;
}

.loginBox {
  width: 600px;
  height: 350px;
  background-color: #f1f3f4;
  border-radius: 3px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.headImg {
  width: 140px;
  height: 140px;
  border-radius: 50%;
  padding: 5px;
  border: 1px solid #ccc;
  box-shadow: 0 0 10px #aaa;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  overflow: hidden;
  img {
    position: absolute;
    top: 0px;
    left: 50%;
    transform: translate(-50%);
    height: 100%;
  }
}

.el-form {
  margin: 100px 50px 0;
  box-sizing: border-box;
  text-align: center;
}
</style>
